#include "cs2nucl.h"
#include <iostream>

/* SOLid Color space coding:

	0: Blue
	1: Green
	2: Orange
	3: Red

   Possible Dinulceotides per color:

	   A C G T   ==>    A C G T
	 ---------        ---------
	A| B G O R       A| 0 1 2 3
	C| G B R O       C| 1 0 3 2
	G| O R B G       G| 2 3 0 1
	T| R O G B       T| 3 2 1 0
 */

/* Convertion tables */
//matrix of SOLiD read translation
int DNA_letter[][4]= {{0, 1, 2, 3}, {1, 0, 3, 2}, {2, 3, 0, 1}, {3, 2, 1, 0}};
// tables of nucleotide coding
char nt_code[] = {'A', 'C', 'G', 'T'};
char nt_code_lower[] = {'a', 'c', 'g', 't'};


/* Convertion function from SOLiD color space to nucleotide.
 * Requires N+1 colors to get N nucleotides. leading 'T' is not included
 * in the result string.
 */
int color2Nucleotide(std::string const cStr, DnaSeq& nSeq, int convLen, int lCnt[4])
{
	int color1, color2, trans;
	int convRes=0;

	/* initialize DNA sequence */
	nSeq.flag=0;
	nSeq.len=0;
	for(int i=0; i < sizeof(nSeq.nucl); i++)
		nSeq.nucl[i]=(char)0;

	/* Check that convertion length requested if not larger that input string length */
	/* Also length of string to convert shall not be null */
	if (convLen >= cStr.length() || convLen <= 0 || cStr.length() == 0 )
		return 1;

	/* Check that first character is 'T' */
	if (cStr[0] != 'T')
		return 1;
	//Change the first T into the rank code associated
	color1=3;
	/* Convert colors */
	for(int i=1; i <= convLen; i++) {
		/* Check input character range (prevents outbound access to convertion
		 * table and allows removing '.' too) */
		if ( cStr[i] != '0' &&
			cStr[i] != '1' &&
			cStr[i] != '2' &&
			cStr[i] != '3' ) {
			convRes=1;
			break;
		}
		/* Get color code */
		color2 = cStr[i] - '0';
		/* Use the translation matrix to extract the rank of the nucleotide */
		trans = DNA_letter[color2][color1];
		/* Add the nucleotid to the translated read */
		nSeq.nucl[NUCL_ARRAY_POS(i-1)] = SET_NUCLEOTIDE(nSeq.nucl[NUCL_ARRAY_POS(i-1)],
				trans, NUCL_CHAR_POS(i-1) );
		nSeq.len++;
		lCnt[trans]++;
		/* For the next iteration, use the rank of the final translated nucleotid */
		color1 = trans;
	}

	return convRes;
	/* Check that first character is 'T' */


}


